import datetime
from typing import Dict

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, text, ForeignKey, JSON, PickleType
from sqlalchemy.orm import Session, relationship

from common.database import Base
from common.database import SessionMixin


class MonitorDorisService(Base, SessionMixin):
    __tablename__ = 'monitor_doris_service'
    service_name = Column(String(128), nullable=False, comment="服务名称")  
    # 应用服务 application|数据库 database|大数据 bigdata
    log_type = Column(String(30), nullable=False, comment="类型")  
    alarm_threshold = Column(Integer, nullable=False, comment="告警阈值")
    # [{"date_range": [0, 1, 2, 3, 4, 5, 6], "start_time": "00:00:00", "end_time": "23:59:59"}]
    alarm_plans = Column(JSON, default=list(), nullable=False, comment="告警计划-时间范围")
    create_time = Column(DateTime, default=datetime.datetime.now, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, nullable=False, comment="更新时间")
    __table_args__ = (
        Index('idx_monitor_doris_service_uuid', 'uuid', unique=True),
        Index('idx_monitor_doris_service_name', 'service_name', unique=True),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return "<uuid='{}',service_name='{}'>".format(self.uuid, self.service_name)

    @classmethod
    def get_service_by_name(cls, db: Session, service_name):
        return db.query(cls).filter(cls.service_name == service_name).to_dict_first()


    @classmethod
    def get_all_service(cls, db: Session):
        return db.query(cls).to_dict()